//
//  HomeScan.swift
//  Scaner
//
//  Created by liyahao on 2025/4/8.
//

import SwiftUI

struct HomeScan: View {
    @State var isPresent = false
    @State var isRecord = false
    @StateObject var vModel = HomeVModel()
    private let grids = Array(repeating: GridItem(.flexible(minimum: 30, maximum: .infinity)), count: 2)
    
    var body: some View {
        NavigationView {
            ScrollView {
                VStack {
                    LazyVGrid(columns: grids, spacing: 10) {
                        ForEach(vModel.models) { model in
                            switch model.type {
                            case .camera:
                                NavigationLink {
                                    Text(model.title)
                                } label: {
                                    CardItem(model: model)
                                }

                            case .docu:
                                NavigationLink {
                                    ScanerListView()
                                } label: {
                                    CardItem(model: model)
                                }
                            case .record:
                                NavigationLink {
                                    RecordListView()
                                } label: {
                                    CardItem(model: model)
                                }
                            case .tts:
                                NavigationLink {
                                    TTSListView()
                                } label: {
                                    CardItem(model: model)
                                }
                            case .translate:
                                NavigationLink {
                                    TransListView()
                                } label: {
                                    CardItem(model: model)
                                }
                            }
                        }
                    }
                    
                    Text("最近使用")
                        .font(.title2.weight(.medium))
                        .foregroundStyle(.red)
                        .frame(maxWidth: .infinity, alignment: .leading)
                        .padding(.vertical)
                    
                    ForEach(vModel.recentModels) { model in
                        RowItem(model: model) {
                            isRecord.toggle()
                        }
                    }
                    
                }
                .padding()
            }
            .navigationTitle("AI扫描王")
        }
    }
}

struct CardItem: View {
    var model: HomeVModel.Model
    var body: some View {
        VStack(spacing: 10) {
            Image(systemName: model.icon)
                .font(.largeTitle.weight(.medium))
                .foregroundStyle(.red)
            
            Text(model.title)
                .font(.system(size: 14).weight(.medium))
                .foregroundStyle(.red)
        }
        .frame(height: 80)
        .frame(maxWidth: .infinity)
        .padding(16)
        .background(Color(.tertiarySystemFill))
        .clipShape(RoundedRectangle(cornerRadius: 10))
        .shadow(color: .black.opacity(0.1), radius: 5)
    }
}

struct RowItem: View {
    var model: HomeVModel.WrpModel
    var action: () -> Void
    
    var body: some View {
        Button {
            action()
        } label: {
            HStack(spacing: 10) {
                Image(systemName: model.model.icon)
                    .font(.largeTitle.weight(.medium))
                    .foregroundStyle(.red)
                VStack(alignment: .leading, spacing: 6) {
                    Text(model.model.title)
                        .font(.title3.weight(.medium))
                        .foregroundStyle(Color(.darkText))
                    Text(model.tsp)
                        .font(.system(size: 14))
                        .foregroundStyle(Color(.systemGray))
                }
                .frame(maxWidth: .infinity, alignment: .leading)
            }
            .frame(height: 40)
            .frame(maxWidth: .infinity)
            .padding(16)
            .clipShape(RoundedRectangle(cornerRadius: 10))
            .shadow(color: .black.opacity(0.1), radius: 5)
        }
    }
}

#Preview {
    HomeScan()
}
